package cn.sytton.taffecloud.common.excel.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * OutputStream工具类
 *
 * @author skyrock
 */
@Slf4j
public class OutputStreamUtil {

//    public static void outputFile(String fileName, HttpServletResponse response) {
//        response.setHeader("content-type", "application/octet-stream");
//        response.setContentType("application/octet-stream");
//        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
//        try {
//            response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
//        } catch (UnsupportedEncodingException e2) {
//            log.error(e2.getMessage(), e2);
//            // 500 ? 200
////            response.setStatus(ExceptionCode);
//            throw new BizException("文件名字转换异常");
//        }
//        byte[] buff = new byte[1024];
//        BufferedInputStream bis = null;
//        OutputStream os = null;
//        try {
//            os = response.getOutputStream();
//            bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
//            int i = bis.read(buff);
//            while (i != -1) {
//                os.write(buff, 0, buff.length);
//                os.flush();
//                i = bis.read(buff);
//            }
//        } catch (FileNotFoundException e1) {
//            log.error(e1.getMessage(), e1);
//            // 500 ? 200
////            response.setStatus(ExceptionCode.BIZ);
//            throw new BizException("没有找到文件");
//        } catch (IOException e) {
//            log.error(e.getMessage(), e);
//            // 500 ? 200
////            response.setStatus(ExceptionCode.BIZ);
//            throw new BizException("下载文件异常");
//        } finally {
//            if (bis != null) {
//                try {
//                    bis.close();
//                } catch (IOException e) {
//                    log.error(e.getMessage(), e);
//                }
//            }
//        }
//    }
//
//    public static void outputFile(String pathName, String fileName, HttpServletResponse response) {
//        File f = new File(pathName, fileName);
//        outputFile(pathName + File.separator + fileName, response);
//    }
//
//    public static void outputImage(String fileName, HttpServletResponse response) {
//        String contentType = "image/jpg";
//        String[] fileInfo = fileName.split("\\.");
//        if (fileInfo.length > 1) {
//            if (fileInfo[1].equals("png")) {
//                contentType = "image/png";
//            }
//        }
//        try {
//            FileInputStream inputStream = new FileInputStream(new File(fileName));
//            byte[] bytes = new byte[inputStream.available()];
//            response.setContentType(contentType);
//            OutputStream out = response.getOutputStream();
//            out.write(bytes);
//            out.flush();
//            out.close();
//        } catch (FileNotFoundException e1) {
//            log.error(e1.getMessage(), e1);
//            // 500 ? 200
////            response.setStatus(ExceptionCode.BIZ);
//            throw new BizException("没有找到文件");
//        } catch (IOException e) {
//            log.error(e.getMessage(), e);
//            // 500 ? 200
////            response.setStatus(ExceptionCode.BIZ);
//            throw new BizException("下载文件异常");
//        }
//    }
//
//    public static void outputImage(String pathName, String fileName, HttpServletResponse response) {
//        File f = new File(pathName, fileName);
//        outputImage(pathName + File.separator + fileName, response);
//    }

    /**
     * 设置下载response头属性
     * @param response
     * @param filename
     * @param length
     */
    public static void setDownloadHeader(HttpServletResponse response, String filename, long length) {
        setDownloadHeader(response, filename, length, true);
    }

    /**
     * 设置下载response头属性
     * @param response
     * @param filename
     * @param length
     */
    public static void setDownloadHeader(HttpServletResponse response, String filename, long length, boolean isOctetStream ) {
        String name;
        try {
            name = URLEncoder.encode(filename, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            log.error("文件名编码错误:"+e.getMessage(), e);
            name = filename;
        }
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.addHeader("Content-Disposition", "attachment;filename=" + name);
        if (isOctetStream) {
            response.setContentType("application/octet-stream");
        }
        if (length > 0) {
            response.addHeader("Content-Length", "" + length);
        }
    }
}
